package com.hziee.cosmanage.companymanage.service.impl;

import com.hziee.common.utils.R;
import com.hziee.cosmanage.companymanage.entity.CompanyEntity;
import com.hziee.cosmanage.companymanage.feign.SearchFeignService;
import com.hziee.cosmanage.companymanage.service.CompanyService;
import com.hziee.cosmanage.companymanage.vo.InviteVo;
import com.hziee.cosmanage.companymanage.vo.InviteVoNew;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.common.utils.PageUtils;
import com.hziee.common.utils.Query;

import com.hziee.cosmanage.companymanage.dao.InviteDao;
import com.hziee.cosmanage.companymanage.entity.InviteEntity;
import com.hziee.cosmanage.companymanage.service.InviteService;
import org.springframework.transaction.annotation.Transactional;


@Service("inviteService")
public class InviteServiceImpl extends ServiceImpl<InviteDao, InviteEntity> implements InviteService {

    @Autowired
    SearchFeignService searchFeignService;

    @Autowired
    CompanyService companyService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<InviteEntity> page = this.page(
                new Query<InviteEntity>().getPage(params),
                new QueryWrapper<InviteEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void accessInvite(Integer cId, Integer inviteId) {
        InviteEntity one = this.getOne(new QueryWrapper<InviteEntity>().eq("company_id", cId).eq("company_invite_id", inviteId));
        one.setStatus(1);
        this.updateById(one);
    }

    @Transactional
    @Override
    public int upInviteRequire(List<InviteEntity> entities) throws IOException {
        Integer companyId = entities.get(0).getCompanyId();
        CompanyEntity byId = companyService.getById(companyId);
        List<InviteVoNew> collect = entities.stream().map(inviteEntity -> {
            inviteEntity.setStatus(2);
            this.updateById(inviteEntity);
            InviteVo inviteVo = new InviteVo();
            BeanUtils.copyProperties(inviteEntity, inviteVo);
            inviteVo.setCompany(byId.getCompanyName());
            InviteVoNew inviteVoNew = new InviteVoNew();
            BeanUtils.copyProperties(inviteVo,inviteVoNew);
            inviteVoNew.setPay(Double.parseDouble(inviteVo.getPay()));
            System.out.println("vo"+inviteVo.getCompanyInviteId());
            return inviteVoNew;
        }).collect(Collectors.toList());
        System.out.println("collect"+collect);
        R r = searchFeignService.inviteStatusUp(collect);
        if (r.getCode()==0) {
            return 1;
        }else {
            return 0;
        }
    }


}